package com.changgou.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * 获取用户访问的ip-过滤器
 */
@Component
public class IPFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("第一个过滤器执行了");
        InetSocketAddress remoteAddress = exchange.getRequest().getRemoteAddress();
        String hostName = remoteAddress.getHostName();
        System.out.println("hostName = " + hostName);
        String hostString = remoteAddress.getHostString();
        System.out.println("hostString = " + hostString);
        InetAddress address = remoteAddress.getAddress();
        System.out.println("address = " + address);
        return chain.filter(exchange);
    }

    /**
     * 设置过滤器执行的优先级
     * @return 返回的int数值越小，越优先执行
     */
    @Override
    public int getOrder() {
        return 1;
    }
}
